<template>
  <div class="login-container">
    <p class="logo-title">PET FOR LIFE</p>
    <div class="logo-tips">
      <div class="left">
        <p>官方客服</p>
        <p class="time">9:00-17:30</p>
      </div>
      <p class="right">400-997-6680</p>
    </div>
  <el-form :model="loginForm" :rules="rules" ref="loginForm" class="login-form" autocomplete="on" label-position="left">
    <div class="title-container">
      <h3 class="title">宠物善终门店管理系统</h3>
    </div>
    <el-form-item prop="phone">
      <el-input
        v-model="loginForm.phone"
        placeholder="请输入您的手机号"
        clearable
      ></el-input>
    </el-form-item>
    <el-form-item  prop="code" >
          <el-input
            v-model="loginForm.code"
            placeholder="请输入验证码"
            clearable
          ></el-input>
          <el-button
          style=""
            :disabled="countdown > 0"
            @click="sendCode"
            type="primary"
            class="erp-send-code-btn"
          >
            {{ countdown > 0? `${countdown}s 后重发` : "发送验证码" }}
          </el-button>
    </el-form-item>
    
      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:10px;" @click.native.prevent="submitForm">登录</el-button>

      <el-button :loading="loading"  style="width:100%;margin-left:0px;" @click.native.prevent="goToPasswordLogin">使用密码登录</el-button>


  
  </el-form>
  </div>
</template>
<script>
export default {
  name: 'ERPloginPage',
  data() {
    return {
      loginForm: {
        phone: '',
        code: ''
      },
      countdown: 0,
      timer: null,
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ]
      },
      loading: false
    };
  },
  methods: {
    sendCode() {
      debugger
      if (this.countdown > 0) return;

      this.$refs.loginForm.validateField("phone", (valid) => {
        if (valid !== "请输入手机号") {
          this.countdown = 60;
          this.timer = setInterval(() => {
            if (this.countdown <= 0) {
              clearInterval(this.timer);
            } else {
              this.countdown--;
            }
          }, 1000);
          // 调用后端接口发送验证码
          this.loading = true;
          this.$store
            .dispatch("user/sendCode", this.loginForm)
            .then((res) => {
              // this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false;
            
              // this.$message.success("验证码已发送，请注意查收");
            })
            .catch(() => {
              this.loading = false;
              // this.$message.error("发送验证码失败，请重试");
            });
          // this.$http.post('/api/sendVerificationCode', { phone: this.loginForm.phone })
          //  .then(() => {
          //     this.$message.success('验证码已发送，请注意查收');
          //   })
          //  .catch(error => {
          //     this.$message.error('发送验证码失败，请重试');
          //     clearInterval(this.timer);
          //     this.countdown = 0;
          //   });
        } else {
        }
      });
    },
    submitForm() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/phoneLogin', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    goToPasswordLogin() {
      // 跳转到密码登录页面，假设密码登录页面的路由为 '/password-login'
      this.$router.push('//login');
    }
  },
  beforeDestroy() {
    clearInterval(this.timer);
  }
};
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */


.login-container {
  .logo-title{
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    position:fixed;
    top:30px;
    left:70px;
  }
  .logo-tips {
    color: #fff;
    display: flex;
    position: fixed;
    bottom: 40px;
    right: 60px;

    .left {
      font-size: 12px;
      padding: 6px 10px 0 0;
      .time {
        font-size: 10px;
      }
    }
    .right {
      font-size: 28px;
      font-weight: 400;
    }
    p {
      padding: 0;
      margin: 0;
    }
  }
  .login-form {
    .el-form-item__content{
      display: flex ;
      width: 100%;
    }
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 2px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .btnsBottom{
    display: flex;
    justify-content: space-between;
    button{
      width: 48%;
    }
  }
  .forgetPassword{
        display: flex;
        justify-content: end;
        margin-bottom: 10px;
        color: #ddd5d5;
        font-size: 14px;
        :hover{
         color: #eee;
        }
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background: url('https://file.petforlife.cn/image/bg.jpg') no-repeat ;
background-size: cover;
  
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
    .el-form-item__content{
      display: flex ;
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }
.btns{
display: flex;
justify-content: center;
}
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    text-align: right;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
